package com.example.zbh.dsliao.ui.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.LocalMusic;
import com.example.zbh.dsliao.utils.FormatTime;

import java.util.List;

import de.greenrobot.event.EventBus;

public class LocalMusicListViewAdapter extends BaseAdapter {

    public TextView music_position, music_title, music_artist, music_duration;    //位置 歌名 歌手  时长

    private Context context;        //上下文对象引用
    public List<LocalMusic> localMusics;  //存放Mp3Info引用的集合
    private LocalMusic localMusic;        //Mp3Info对象引用
    private int position = 0;           //当前正在播放的位置


    public LocalMusicListViewAdapter(Context context, List<LocalMusic> localMusics) {
        this.context = context;
        this.localMusics = localMusics;
    }


    public LocalMusicListViewAdapter(Context context, List<LocalMusic> localMusics, int position){
        this.context = context;
        this.localMusics = localMusics;
        this.position = position;
        EventBus.getDefault().register(this);
    }

    /**
     * 根据数据集合个数返回  控制item条数, 即数据控制列表个数
     * **/
    @Override
    public int getCount() {
        return localMusics != null && localMusics.size() > 0 ? localMusics.size() : 0;
    }


    /**
     * 返回当前下标所对应的行视图
     * **/
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.list_local_music, null);
            music_position = (TextView) convertView.findViewById(R.id.tv_local_position);
            music_title = (TextView)convertView.findViewById(R.id.music_title);
            music_artist = (TextView)convertView.findViewById(R.id.music_artist);
            music_duration = (TextView)convertView.findViewById(R.id.music_duration);


//            //设置选中与未选中的颜色
//            if (this.position == position) {
//                music_artist.setTextColor(context.getResources().getColor(R.color.color_select_list));
//                music_title.setTextColor(context.getResources().getColor(R.color.color_select_list));
//            } else {
//                music_artist.setTextColor(context.getResources().getColor(R.color.color_grey));
//                music_title.setTextColor(context.getResources().getColor(R.color.color_black));
//            }
//
        }
        LocalMusic localMusic = localMusics.get(position);

        //设置编号
        if (position + 1 < 10) {
            music_position.setText("0" + (position + 1));
        } else{
            music_position.setText("" + (position + 1));
        }

        music_title.setText(localMusic.getTitle());         //显示标题
        music_artist.setText(localMusic.getArtist());       //显示艺术家
        music_duration.setText(String.valueOf(FormatTime.formatTime(localMusic.getDuration()))); //显示长度
        return convertView;
    }

    /**
     * 返回当前下标(position)数据所对应的id
     * **/
    @Override
    public long getItemId(int position) {
        return position;
    }


    /**
     * 返回当前下标(position)所对应的数据
     * **/
    @Override
    public Object getItem(int position) {
        return localMusics != null && localMusics.size() > 0 ? localMusics.get(position) : 0;
    }




}

